package main.java.com.negromotad.parser;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

public class TMDataLoader {

	private Vector<String> vars;
	private Double[][] entry;
	private int rows;
	private int cols;

	public static void main(String[] args) {
		new TMDataLoader().load("D:\\data.txt");
	}

	public void load(String pathToTxt) {
		BufferedReader r;
		try {
			String s = "";
			vars = new Vector<String>();
			r = new BufferedReader(new FileReader(pathToTxt));
			String[] vars = r.readLine().split("\\s+");
			int i = 0;
			while (i < vars.length) {
				if ((vars[i] != null) && !"".equals(vars[i])) {
					this.vars.add(vars[i]);
				}
				i++;
			}
			Double[][] auxEntry = new Double[10000][20];
			rows = 0;
			while ((s = r.readLine()) != null) {
				String[] numbers = s.split("\\s+");
				// Avoid row number. Start column 1.
				for (int j = 1; j < numbers.length; j++) {
					String number = numbers[j].replace(",", ".");
					auxEntry[rows][j - 1] = Double.parseDouble(number);
				}
				// showRow(rows);
				cols = numbers.length - 1;
				rows++;

			}
			entry = new Double[rows][cols];
			for (int j = 0; j < rows; j++) {
				for (int j2 = 0; j2 < cols; j2++) {
					entry[j][j2] = auxEntry[j][j2];
				}

			}
		} catch (FileNotFoundException e) {
			System.out.println("File not found: " + e);
		} catch (IOException e) {
			System.out.println("There was a problem reading from " + pathToTxt
					+ " cause: " + e);
			;
		} catch (NumberFormatException e) {
			System.out.println("Wrong number entry. " + e);
		}
	}

	private void showRow(int rows2) {
		for (int i = 0; i < 3; i++) {
			System.out.print(entry[rows2][i] + " | ");
		}
		System.out.println("");

	}

	public Vector<String> getVars() {
		return vars;
	}

	public Double[][] getEntry() {
		return entry;
	}

	public int getRows() {
		return rows;
	}

	public int getCols() {
		return cols;
	}

}
